package org.dromara.business.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.business.domain.BusinessInformation;
import org.dromara.business.domain.User;
import org.dromara.business.domain.bo.UserBo;
import org.dromara.business.domain.vo.BusinessInformationVo;
import org.dromara.business.domain.vo.UserVo;
import org.dromara.business.service.BusinessInformationService;
import org.dromara.business.service.UserService;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.translation.annotation.Translation;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.vo.SysUserVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import java.util.Date;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户信息
 *
 * @author zlj
 * @date 2024-11-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/user")
public class UserController extends BaseController {
    private final BusinessInformationService businessInformationService;
    private final UserService UserService;

    /**
     * 查询用户信息列表
     */
    //@SaCheckPermission("business:user:list")
    @GetMapping("/list")
    public TableDataInfo<UserVo> list(UserBo bo, PageQuery pageQuery) {
        bo.setUserType("xcx_user");
        return UserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户信息列表
     */
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserBo bo, HttpServletResponse response) {
        List<UserVo> list = UserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户信息", UserVo.class, response);
    }

    /**
     * 获取用户信息详细信息
     *
     * @param userId 主键
     */
    //@SaCheckPermission("business:user:query")
    @GetMapping("/{userId}")
    public R<UserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(UserService.queryById(userId));
    }

    /**
     * 新增用户信息
     */
   // @SaCheckPermission("business:user:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add( @RequestBody UserBo bo) {
        return toAjax(UserService.insertByBo(bo));
    }

    /**
     * 修改用户信息
     */
    //@SaCheckPermission("business:user:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@RequestBody UserBo bo) {
        return toAjax(UserService.updateByBo(bo));
    }

    /**
     * 上传简历
     * @param bo
     * @return
     */
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/uploadResume")
    public R<Void> uploadResume(@RequestBody UserBo bo) {
        return toAjax(UserService.updateByBo(bo));
    }

    /**
     * 删除用户信息
     *
     * @param userIds 主键串
     */
    //@SaCheckPermission("business:user:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(UserService.deleteWithValidByIds(List.of(userIds), true));
    }


    /**
     * 判断当前用户是否会员
     */
    //@SaCheckPermission("business:user:query")
    @GetMapping("/ifMember")
    public R<Boolean> ifMember() {
        Long userId = LoginHelper.getUserId();
        User user = UserService.getById(userId);
        if (user!=null){
            if (user.getHyNumber()>0 && DateUtil.compare(new Date(),user.getGqTime()) <= 0){
                return R.ok(true);
            }
        }
        return R.ok(false);
    }

    /**
     * 查看手机号
     */
    //@SaCheckPermission("business:user:query")
    @GetMapping("/viewPhone")
    public R<String> viewPhone(String iId) {
        Long userId = LoginHelper.getUserId();
        User user = UserService.getById(userId);
        BusinessInformation information = businessInformationService.getById(iId);

        if (user!=null){
            if (user.getHyNumber()>0 && DateUtil.compare(new Date(),user.getGqTime()) <= 0){
                UserService.updateById(new User(){{
                    setUserId(userId);
                    setHyNumber(user.getHyNumber()-1);
                }});
                if (!"".equals(information.getLxrDh()) && information.getLxrDh()!=null){
                    return R.data(information.getLxrDh());
                }
            }
        }
        return R.fail("查看次数用尽或会员已过期");
    }

    @GetMapping("/getMyInfo")
    public R<UserVo> getMyInfo() {
        User user = UserService.getById(LoginHelper.getUserId());
        if (user!=null){
            if (user.getHyNumber()>0 && DateUtil.compare(new Date(),user.getGqTime()) <= 0){
                user.setIfhy(true);
            }
        }
        return R.ok(BeanUtil.toBean(user, UserVo.class));
    }
}
